package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/7/8 21:28
 * @description：https://leetcode-cn.com/problems/binary-subarrays-with-sum/
 */
public class 和相同的二元子数组 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Solution {
        public int numSubarraysWithSum(int[] nums, int goal) {
            int sum = 0;
            HashMap<Integer, Integer> cnt = new HashMap<>();
            int ret = 0;
            for (int num : nums) {
                cnt.put(sum, cnt.getOrDefault(sum, 0) + 1);
                sum += num;
                ret += cnt.getOrDefault(sum - goal, 0);
            }
            return ret;
        }
    }

}
